const {baseModel} = require("../../js/service/baseModel.js");

module.exports = {
    // http://localhost:9023/manage/exampleUseBaseModel/getAll
    getAll: async (request, response) => {
        let sql = 'select id,name,price from mo_goods where id>75 order by id desc';
        let res = await baseModel.getAll(sql)
        // if (res.code === config.result.success.code) {
        //     return response.success(res);
        // } else {
        //     return response.error(res)
        // }
        return response.showResult(res)
    },

    // http://localhost:9023/manage/exampleUseBaseModel/getDataById?id=70
    getDataById: async (request, response) => {
        let {id} = request.query;
        let sql = 'select id, name, price from mo_goods';
        let res = await baseModel.getDataById(sql, id);
        return response.showResult(res)
    },

    // http://localhost:9023/manage/exampleUseBaseModel/add
    // post {"name":"商品名称","price":111}
    add: async (request, response) => {
        let res = await baseModel.add('mo_goods', request.body);
        return response.showResult(res)
    },

    // http://localhost:9023/manage/exampleUseBaseModel/updateById
    // post {"id":70,"name":"商品名称","price":111}
    updateById: async (request, response) => {
        let res = await baseModel.updateById('mo_goods', request.body);
        return response.showResult(res)
    },

    // http://localhost:9023/manage/exampleUseBaseModel/getPagination
    // post {"page":1,"size":10}
    getPagination: async (request, response) => {
        const page = parseInt(request.body.page) || 1;
        const size = parseInt(request.body.size) || 10;
        let totalSql = "SELECT COUNT(*) AS total FROM mo_goods";
        let sql = 'select id, name, price from mo_goods';
        let res = await baseModel.getPagination(sql, totalSql, page, size);
        return response.showResult(res)
    },

    // http://localhost:9023/manage/exampleUseBaseModel/realDel?id=120
    realDel: async (request, response) => {
        let {id} = request.query;
        let res = await baseModel.realDelById('mo_goods', id);
        return response.showResult(res)
    },

}